<?php
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/settings_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/account_mod.php");
	require_once($_SERVER["DOCUMENT_ROOT"]."/modules/base_mod.php");

	class SecurityMod {
		public static function checkLogin() {
			$name = v($_REQUEST, "acc");
			$password = v($_REQUEST, "pwd");
			if (!isset($name) || !isset($password)) {
				return null;
			}
			$acc = AccountMod::getAccount($name);
			if (is_null($acc) || $acc->getPassword() != $password) {
				return null;
			} else
				return $acc;
		}

		public static function checkHackerLogin() {
			$name = v($_REQUEST, "acc");
			$password = v($_REQUEST, "pwd");
			if (!isset($name) || !isset($password)) {
				return null;
			}
			$acc = AccountMod::getHackerAccount($name);
			if (is_null($acc) || $acc->getPassword() != $password) {
				return null;
			} else
				return $acc;
		}
                
		public static function checkAutoLogin() {
                        $name = v($_REQUEST, "uname");
			$pilotId = v($_REQUEST, "userid");
			$validation= v($_REQUEST, "vcode");
			if (!isset($pilotId) || !isset($validation)) {
				return null;
			}
			$acc = AccountMod::getAfmAccount($pilotId);

			if (is_null($acc) || $acc->getValidation() != $validation) {
				AccountMod::logFailedLogin($name);
				return null;
			} else
				return $acc;
		}
                
		public static function checkChatLogin($name, $password) {
			if (!isset($name) || !isset($password)) {
				return null;
			}
			$acc = AccountMod::getChatAccount($name, $password);

			if (is_null($acc)) {
				return null;
			} else
				return $acc;
		}
                
		public static function autoLogin() {
			session_name(SettingsMod::SESSION_NAME);
			session_start();
			if (!isset($_SESSION["account"])) {
				$acc = self::checkAutoLogin();
				if (is_null($acc)) {
					self::logout();
				}
				session_regenerate_id(true);
				$_SESSION["account"] = $acc;
			}
			session_write_close();
		}
                
		public static function loginChatUser($name, $password) {
                        $acc = self::checkChatLogin($name, $password);
                        $_SESSION["account"] = $acc;
			session_write_close();
		}
                
		public static function loginDynaScript() {
			session_name("DynaScript");
			session_start();
			if (!isset($_SESSION["account"])) {
				$acc = self::checkAutoLogin();
				if (is_null($acc)) {
					self::logout();
				}
				session_regenerate_id(true);
				$_SESSION["account"] = $acc;
			}
			session_write_close();
		}

		public static function login() {
			session_name(SettingsMod::SESSION_NAME);
			session_start();
			if (!isset($_SESSION["account"])) {
				$acc = self::checkLogin();
				if (is_null($acc)) {
					self::logout();
				}
				session_regenerate_id(true);
				$_SESSION["account"] = $acc;
			}
			session_write_close();
			$conn = BaseMod::getConnection();
			$user=$_SESSION["account"]->getName();
			$date=date('c');
			$ip = $_SERVER['REMOTE_ADDR'];
			mysql_query("replace into navigation (username,time,IP) values('$user','$date','$ip')");
		}

		public static function logout() {
			session_name(SettingsMod::SESSION_NAME);
			session_start();
			$_SESSION = array();
			if (isset($_COOKIE[session_name()])) {
				setcookie(session_name(), "", time() - 60 * 60, "/");
			}
			session_destroy();
			header("Location: login.php");
			exit();
		}
		
		public static function logoutDynaScript() {
			session_name("DynaScript");
			session_start();
			$_SESSION = array();
			if (isset($_COOKIE[session_name()])) {
				setcookie(session_name(), "", time() - 60 * 60, "/");
			}
			session_destroy();
			exit();
		}
                
		public function generatePassword($length=10) {
				$vowels = 'aeuyAEUY23456789';
				$consonants = 'bdghjmnpqrstvzBDGHJLMNPQRSTVWXZ';

				$password = '';
				$alt = time() % 2;
				for ($i = 0; $i < $length; $i++) {
						if ($alt == 1) {
								$password .= $consonants[(rand() % strlen($consonants))];
								$alt = 0;
						} else {
								$password .= $vowels[(rand() % strlen($vowels))];
								$alt = 1;
						}
				}
				return $password;
		}
	}
?>